package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.text.DateFormat;

public class NewOrder extends JFrame {

	private JPanel contentPane;
	private JTextField txtDelDay;
	private JTextField txtID;
	private JTextField txtCustomer;
	private JTextField txtPayDay;
	private JTable TableOP;
	private Controller ctr;
	private int total;
	private AddProdToOrder addpto;
	private String date;

	
	/**
	 * Create the frame.
	 */
	public NewOrder() {
		setResizable(false);
		setTitle("New order");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 230);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 85, 14);
		contentPane.add(lblId);
		
		JLabel lblCustomer = new JLabel("Customer:");
		lblCustomer.setBounds(10, 36, 85, 14);
		contentPane.add(lblCustomer);
		
		JLabel lblDeliverydate = new JLabel("DeliveryDate:");
		lblDeliverydate.setBounds(10, 61, 85, 14);
		contentPane.add(lblDeliverydate);
		
		JLabel lblPaymentDay = new JLabel("Payment day:");
		lblPaymentDay.setBounds(10, 86, 85, 14);
		contentPane.add(lblPaymentDay);
		
		txtDelDay = new JTextField();
		txtDelDay.setBounds(105, 58, 86, 20);
		contentPane.add(txtDelDay);
		txtDelDay.setColumns(10);
		
		txtID = new JTextField();
		txtID.setBounds(105, 8, 86, 20);
		contentPane.add(txtID);
		txtID.setColumns(10);
		
		txtCustomer = new JTextField();
		txtCustomer.setBounds(105, 33, 86, 20);
		contentPane.add(txtCustomer);
		txtCustomer.setColumns(10);
		
		txtPayDay = new JTextField();
		txtPayDay.setBounds(105, 83, 86, 20);
		contentPane.add(txtPayDay);
		txtPayDay.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(212, 11, 222, 119);
		contentPane.add(scrollPane);
		
		TableOP = new JTable();
		TableOP.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Name", "Quantity", "Price"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, Integer.class, Double.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		TableOP.getColumnModel().getColumn(0).setResizable(false);
		TableOP.getColumnModel().getColumn(1).setResizable(false);
		TableOP.getColumnModel().getColumn(2).setResizable(false);
		scrollPane.setViewportView(TableOP);
		
		JButton btnAddProducts = new JButton("Add products");
		btnAddProducts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addpto = new AddProdToOrder();
				addpto.setVisible(true);
			}
		});
		btnAddProducts.setBounds(94, 107, 97, 23);
		contentPane.add(btnAddProducts);
		
		JButton button = new JButton("Back to previous");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		button.setBounds(10, 166, 118, 23);
		contentPane.add(button);
		

		date = DateFormat.getInstance().format(new Date());
		date = date.substring(0, 8);
		
		total = 1;
		
		JButton button_1 = new JButton("Place order");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addOrderToDB(txtID.getText(), txtCustomer.getText(), txtDelDay.getText(), txtPayDay.getText(), total, date);
			}
		});
		button_1.setBounds(316, 166, 118, 23);
		contentPane.add(button_1);
		
		
		JLabel lblDate = new JLabel("Date: "  + date);
		lblDate.setBounds(10, 141, 144, 14);
		contentPane.add(lblDate);
		
		
		
		JLabel lblTotal = new JLabel("Total:  " + total);
		lblTotal.setBounds(290, 141, 144, 14);
		contentPane.add(lblTotal);
	}

	
	private void addOrderToDB(String id, String cusID, String delDay, String payDay, int total, String date)
	{
		{
			if(id.isEmpty() || cusID.isEmpty() || delDay.isEmpty() || payDay.isEmpty())
			{
				JOptionPane.showMessageDialog(null,
					    "All fields have to be filled.",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				ctr.newOrder(id, cusID, delDay, payDay, total, date);
				//table at customer window.addElement(/*???*/);
				dispose();
				
			}	
		}
	}	
}
